/*
* @Author: Administrator
* @Date:   2018-01-13 11:29:24
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-27 16:25:14
*/
import { observable, action, toJS } from 'mobx'
import { topicDetail, collect, deCollect, newReply } from '../constant/api'
import { get, post } from '../util/request'
import objToStrMap from '../util/toMap'

class TopicsDetailStore {
  constructor(topicsDetail) {
    this.topicDetail = objToStrMap(topicsDetail.topicDetail) || new Map()
    this.hasContent = topicsDetail.hasContent
  }
  @observable topicDetail
  @observable hasContent
  @action getTopicDetail(topicId) {
    return new Promise((resolve, reject) => {
      const key = topicId
      if (this.hasContent.indexOf(key) !== -1) {
        resolve()
        return
      }
      const topicDetailUrl = `${topicDetail}/${topicId}`
      get(topicDetailUrl, { mdrender: false }).then((res) => {
        if (res.status === 200 && res.data.success) {
          this.topicDetail.set(key, res.data.data)
          this.hasContent.push(key)
          resolve()
        } else {
          reject(res.data)
        }
      }).catch(reject)
    })
  }
  @action collect(topicId) {
    return new Promise((resolve, reject) => {
      const params = {
        needToken: true,
        topic_id: topicId,
      }
      post(collect, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          resolve()
        } else {
          reject(res.data.message)
        }
      }).catch(e => alert(e))
    })
  }
  @action deCollect(topicId) {
    return new Promise((resolve, reject) => {
      const params = {
        needToken: true,
        topic_id: topicId,
      }
      post(deCollect, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          resolve()
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => alert(e))
    })
  }
  @action upReply(replyId) {
    const url = `/reply/${replyId}/ups`
    const params = {
      needToken: true,
    }
    return new Promise((resolve, reject) => {
      post(url, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          resolve(res.data.action)
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => alert(e))
    })
  }
  @action createReply(topicId, content) {
    const url = newReply(topicId)
    const params = {
      needToken: true,
      content,
    }
    return new Promise((resolve, reject) => {
      post(url, params).then((res) => {
        if (res.status === 200 && res.data.success) {
          const index = this.hasContent.indexOf(topicId)
          this.hasContent.splice(index, 1)
          this.getTopicDetail(topicId).then(() => resolve()).catch(reject)
        } else {
          reject(res.data.error_msg)
        }
      }).catch(e => alert(e))
    })
  }
  toJson() {
    return {
      topicDetail: toJS(this.topicDetail),
      hasContent: toJS(this.hasContent),
    }
  }
}

export default TopicsDetailStore
